//
//  LoginViewController.m
//  FindJob
//
//  Created by 233 on 16/2/29.
//  Copyright © 2016年 Lee. All rights reserved.
//

#import "LoginViewController.h"
#import <CoreText/CoreText.h>
#import "LoginAPIManger.h"
#import "UserModel.h"
#import "WaitAlertView.h"
#import "AlertView.h"


typedef void(^userInfoBlock)(id,id);

@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet UIButton *registerBtn;
@property (weak, nonatomic) IBOutlet UIButton *forgetPasswold;

@property (weak, nonatomic) IBOutlet UITextField *accountTextField;

@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property(nonatomic,copy)userInfoBlock userInfoBlock;

@property(nonatomic,strong)LoginAPIManger *loginManger;
@end

@implementation LoginViewController

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    UserModel *model = [UserModel shareUserModel];
    [model readAccountAndPassword];
    self.accountTextField.text = model.account;
    self.passwordTextField.text = model.password;
    
    if (model.isRegisterSuccess) {
            [self showAlertViewControllerWithTarget:self title:@"恭喜" message:@"注册成功"];
        }
        

    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    NSMutableAttributedString *str1 = [[NSMutableAttributedString alloc] initWithString:@"10秒快速注册"];
    NSRange strRange1 = {0,[str1 length]};
    [str1 addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:strRange1];
    [_registerBtn setAttributedTitle:str1 forState:UIControlStateNormal];
    
    NSMutableAttributedString *str2 = [[NSMutableAttributedString alloc] initWithString:@"忘记密码"];
    NSRange strRange2 = {0,[str2 length]};
    [str2 addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:strRange2];
    [_forgetPasswold setAttributedTitle:str2 forState:UIControlStateNormal];

}

#pragma mark *** private ***
- (void)showAlertViewControllerWithTarget:(id _Nonnull)target title:(NSString *_Nonnull)title message:(NSString *_Nonnull)message{
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
    [target presentViewController:alertController animated:YES completion:nil];
}
- (BOOL)judgeTextFieldIsVaild:(UITextField *)textField{
    if (textField.text.length > 0) {
        return YES;
    }else{
        return NO;
    }
}


- (IBAction)loginBtn:(UIButton *)sender {
    
    
    
    if ([self judgeTextFieldIsVaild:self.accountTextField] && [self judgeTextFieldIsVaild:self.passwordTextField]) {
        NSDictionary *parameter = @{@"userName":self.accountTextField.text,
                                    @"password":self.passwordTextField.text};
        // 显示等待界面
        WaitAlertView *waitView = [[WaitAlertView alloc] init];
        [waitView show];
        __weak LoginViewController *myself = self;
        
       [self.loginManger startLoginRequestParameter:parameter responds:^(RespondsStatus status, NSString *message) {
           // 隐藏等待界面
           [waitView hide];
           if (status == RespondsStatusDataError) {
               //
           }else{
               AlertView *alertView = [[AlertView alloc] initWithTitle:message];
               [alertView show];
               if (status == RespondsStatusSuccess) {
                   UserModel *userModel =  [UserModel shareUserModel];
                   
                   userModel.isLogin = YES;
                   userModel.account = myself.accountTextField.text;
                   userModel.password = myself.passwordTextField.text;
                   [userModel saveAccountAndPassword];
                   
                   [myself.navigationController popViewControllerAnimated:YES];
               }
               
           }
           NSLog(@"message = %@",message);
       }];
       
    }else{
        NSLog(@"登录失败");
    }
}
#pragma mark *** UIbutton action ***
- (IBAction)unwindToMainViewController:(UIStoryboardSegue *)unwindSegue{
    
}
#pragma mark *** getter ***
- (LoginAPIManger *)loginManger{
    if (!_loginManger) {
        _loginManger = [[LoginAPIManger alloc] init];
    }
    return _loginManger;
}
@end
